<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pgmpy.factors.discrete.DiscreteFactor &#8212; pgmpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../../../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../../index.html">
              <img class="logo" src="../../../../_static/logo.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for pgmpy.factors.discrete.DiscreteFactor</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">division</span>

<span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">product</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">namedtuple</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">pgmpy.factors.base</span> <span class="k">import</span> <span class="n">BaseFactor</span>
<span class="kn">from</span> <span class="nn">pgmpy.extern</span> <span class="k">import</span> <span class="n">tabulate</span>
<span class="kn">from</span> <span class="nn">pgmpy.extern</span> <span class="k">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">pgmpy.extern.six.moves</span> <span class="k">import</span> <span class="nb">map</span><span class="p">,</span> <span class="nb">range</span><span class="p">,</span> <span class="n">reduce</span><span class="p">,</span> <span class="nb">zip</span>
<span class="kn">from</span> <span class="nn">pgmpy.utils</span> <span class="k">import</span> <span class="n">StateNameInit</span><span class="p">,</span> <span class="n">StateNameDecorator</span>

<span class="n">State</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;State&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;var&#39;</span><span class="p">,</span> <span class="s1">&#39;state&#39;</span><span class="p">])</span>


<div class="viewcode-block" id="DiscreteFactor"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.discrete.DiscreteFactor.DiscreteFactor">[docs]</a><span class="k">class</span> <span class="nc">DiscreteFactor</span><span class="p">(</span><span class="n">BaseFactor</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base class for DiscreteFactor.</span>

<span class="sd">    Public Methods</span>
<span class="sd">    --------------</span>
<span class="sd">    assignment(index)</span>
<span class="sd">    get_cardinality(variable)</span>
<span class="sd">    marginalize([variable_list])</span>
<span class="sd">    normalize()</span>
<span class="sd">    product(*DiscreteFactor)</span>
<span class="sd">    reduce([variable_values_list])</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nd">@StateNameInit</span><span class="p">()</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">variables</span><span class="p">,</span> <span class="n">cardinality</span><span class="p">,</span> <span class="n">values</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initialize a factor class.</span>

<span class="sd">        Defined above, we have the following mapping from variable</span>
<span class="sd">        assignments to the index of the row vector in the value field:</span>

<span class="sd">        +-----+-----+-----+-------------------+</span>
<span class="sd">        |  x1 |  x2 |  x3 |    phi(x1, x2, x3)|</span>
<span class="sd">        +-----+-----+-----+-------------------+</span>
<span class="sd">        | x1_0| x2_0| x3_0|     phi.value(0)  |</span>
<span class="sd">        +-----+-----+-----+-------------------+</span>
<span class="sd">        | x1_0| x2_0| x3_1|     phi.value(1)  |</span>
<span class="sd">        +-----+-----+-----+-------------------+</span>
<span class="sd">        | x1_0| x2_1| x3_0|     phi.value(2)  |</span>
<span class="sd">        +-----+-----+-----+-------------------+</span>
<span class="sd">        | x1_0| x2_1| x3_1|     phi.value(3)  |</span>
<span class="sd">        +-----+-----+-----+-------------------+</span>
<span class="sd">        | x1_1| x2_0| x3_0|     phi.value(4)  |</span>
<span class="sd">        +-----+-----+-----+-------------------+</span>
<span class="sd">        | x1_1| x2_0| x3_1|     phi.value(5)  |</span>
<span class="sd">        +-----+-----+-----+-------------------+</span>
<span class="sd">        | x1_1| x2_1| x3_0|     phi.value(6)  |</span>
<span class="sd">        +-----+-----+-----+-------------------+</span>
<span class="sd">        | x1_1| x2_1| x3_1|     phi.value(7)  |</span>
<span class="sd">        +-----+-----+-----+-------------------+</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        variables: list, array-like</span>
<span class="sd">            List of variables in the scope of the factor.</span>

<span class="sd">        cardinality: list, array_like</span>
<span class="sd">            List of cardinalities of each variable. `cardinality` array must have a value</span>
<span class="sd">            corresponding to each variable in `variables`.</span>

<span class="sd">        values: list, array_like</span>
<span class="sd">            List of values of factor.</span>
<span class="sd">            A DiscreteFactor&#39;s values are stored in a row vector in the value</span>
<span class="sd">            using an ordering such that the left-most variables as defined in</span>
<span class="sd">            `variables` cycle through their values the fastest.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; phi = DiscreteFactor([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;], [2, 2, 2], np.ones(8))</span>
<span class="sd">        &gt;&gt;&gt; phi</span>
<span class="sd">        &lt;DiscreteFactor representing phi(x1:2, x2:2, x3:2) at 0x7f8188fcaa90&gt;</span>
<span class="sd">        &gt;&gt;&gt; print(phi)</span>
<span class="sd">        +------+------+------+-----------------+</span>
<span class="sd">        | x1   | x2   | x3   |   phi(x1,x2,x3) |</span>
<span class="sd">        |------+------+------+-----------------|</span>
<span class="sd">        | x1_0 | x2_0 | x3_0 |          1.0000 |</span>
<span class="sd">        | x1_0 | x2_0 | x3_1 |          1.0000 |</span>
<span class="sd">        | x1_0 | x2_1 | x3_0 |          1.0000 |</span>
<span class="sd">        | x1_0 | x2_1 | x3_1 |          1.0000 |</span>
<span class="sd">        | x1_1 | x2_0 | x3_0 |          1.0000 |</span>
<span class="sd">        | x1_1 | x2_0 | x3_1 |          1.0000 |</span>
<span class="sd">        | x1_1 | x2_1 | x3_0 |          1.0000 |</span>
<span class="sd">        | x1_1 | x2_1 | x3_1 |          1.0000 |</span>
<span class="sd">        +------+------+------+-----------------+</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">variables</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Variables: Expected type list or array like, got string&quot;</span><span class="p">)</span>

        <span class="n">values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">values</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">float</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">cardinality</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">variables</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Number of elements in cardinality must be equal to number of variables&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">values</span><span class="o">.</span><span class="n">size</span> <span class="o">!=</span> <span class="n">np</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="n">cardinality</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Values array must be of size: </span><span class="si">{size}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">size</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="n">cardinality</span><span class="p">)))</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">variables</span><span class="p">))</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">variables</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Variable names cannot be same&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">variables</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">variables</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">cardinality</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">values</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span><span class="p">)</span>

<div class="viewcode-block" id="DiscreteFactor.scope"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.discrete.DiscreteFactor.DiscreteFactor.scope">[docs]</a>    <span class="k">def</span> <span class="nf">scope</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the scope of the factor.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        list: List of variable names in the scope of the factor.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; phi = DiscreteFactor([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;], [2, 3, 2], np.ones(12))</span>
<span class="sd">        &gt;&gt;&gt; phi.scope()</span>
<span class="sd">        [&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">variables</span></div>

<div class="viewcode-block" id="DiscreteFactor.get_cardinality"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.discrete.DiscreteFactor.DiscreteFactor.get_cardinality">[docs]</a>    <span class="k">def</span> <span class="nf">get_cardinality</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">variables</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns cardinality of a given variable</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        variables: list, array-like</span>
<span class="sd">                A list of variable names.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        dict: Dictionary of the form {variable: variable_cardinality}</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; phi = DiscreteFactor([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;], [2, 3, 2], range(12))</span>
<span class="sd">        &gt;&gt;&gt; phi.get_cardinality([&#39;x1&#39;])</span>
<span class="sd">        {&#39;x1&#39;: 2}</span>
<span class="sd">        &gt;&gt;&gt; phi.get_cardinality([&#39;x1&#39;, &#39;x2&#39;])</span>
<span class="sd">        {&#39;x1&#39;: 2, &#39;x2&#39;: 3}</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">variables</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;variables: Expected type list or array-like, got type str&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">([</span><span class="n">var</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">variables</span> <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="n">variables</span><span class="p">]):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Variable not in scope&quot;</span><span class="p">)</span>

        <span class="k">return</span> <span class="p">{</span><span class="n">var</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">var</span><span class="p">)]</span> <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="n">variables</span><span class="p">}</span></div>

    <span class="nd">@StateNameDecorator</span><span class="p">(</span><span class="n">argument</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">return_val</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<div class="viewcode-block" id="DiscreteFactor.assignment"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.discrete.DiscreteFactor.DiscreteFactor.assignment">[docs]</a>    <span class="k">def</span> <span class="nf">assignment</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a list of assignments for the corresponding index.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        index: list, array-like</span>
<span class="sd">            List of indices whose assignment is to be computed</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        list: Returns a list of full assignments of all the variables of the factor.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; phi = DiscreteFactor([&#39;diff&#39;, &#39;intel&#39;], [2, 2], np.ones(4))</span>
<span class="sd">        &gt;&gt;&gt; phi.assignment([1, 2])</span>
<span class="sd">        [[(&#39;diff&#39;, 0), (&#39;intel&#39;, 1)], [(&#39;diff&#39;, 1), (&#39;intel&#39;, 0)]]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">index</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>

        <span class="n">max_possible_index</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">prod</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">(</span><span class="n">i</span> <span class="o">&lt;=</span> <span class="n">max_possible_index</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">index</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s2">&quot;Index greater than max possible index&quot;</span><span class="p">)</span>

        <span class="n">assignments</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">index</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">scope</span><span class="p">())),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">)</span>
        <span class="n">rev_card</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">card</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rev_card</span><span class="p">):</span>
            <span class="n">assignments</span><span class="p">[:,</span> <span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">index</span> <span class="o">%</span> <span class="n">card</span>
            <span class="n">index</span> <span class="o">=</span> <span class="n">index</span> <span class="o">//</span> <span class="n">card</span>

        <span class="n">assignments</span> <span class="o">=</span> <span class="n">assignments</span><span class="p">[:,</span> <span class="p">::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

        <span class="k">return</span> <span class="p">[[(</span><span class="n">key</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span> <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">,</span> <span class="n">values</span><span class="p">)]</span> <span class="k">for</span> <span class="n">values</span> <span class="ow">in</span> <span class="n">assignments</span><span class="p">]</span></div>

<div class="viewcode-block" id="DiscreteFactor.identity_factor"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.discrete.DiscreteFactor.DiscreteFactor.identity_factor">[docs]</a>    <span class="k">def</span> <span class="nf">identity_factor</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the identity factor.</span>

<span class="sd">        Def: The identity factor of a factor has the same scope and cardinality as the original factor,</span>
<span class="sd">             but the values for all the assignments is 1. When the identity factor is multiplied with</span>
<span class="sd">             the factor it returns the factor itself.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        DiscreteFactor: The identity factor.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; phi = DiscreteFactor([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;], [2, 3, 2], range(12))</span>
<span class="sd">        &gt;&gt;&gt; phi_identity = phi.identity_factor()</span>
<span class="sd">        &gt;&gt;&gt; phi_identity.variables</span>
<span class="sd">        [&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;]</span>
<span class="sd">        &gt;&gt;&gt; phi_identity.values</span>
<span class="sd">        array([[[ 1.,  1.],</span>
<span class="sd">                [ 1.,  1.],</span>
<span class="sd">                [ 1.,  1.]],</span>

<span class="sd">               [[ 1.,  1.],</span>
<span class="sd">                [ 1.,  1.],</span>
<span class="sd">                [ 1.,  1.]]])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">DiscreteFactor</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">size</span><span class="p">))</span></div>

<div class="viewcode-block" id="DiscreteFactor.marginalize"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.discrete.DiscreteFactor.DiscreteFactor.marginalize">[docs]</a>    <span class="k">def</span> <span class="nf">marginalize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">variables</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Modifies the factor with marginalized values.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        variables: list, array-like</span>
<span class="sd">            List of variables over which to marginalize.</span>

<span class="sd">        inplace: boolean</span>
<span class="sd">            If inplace=True it will modify the factor itself, else would return</span>
<span class="sd">            a new factor.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        DiscreteFactor or None: if inplace=True (default) returns None</span>
<span class="sd">                        if inplace=False returns a new `DiscreteFactor` instance.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; phi = DiscreteFactor([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;], [2, 3, 2], range(12))</span>
<span class="sd">        &gt;&gt;&gt; phi.marginalize([&#39;x1&#39;, &#39;x3&#39;])</span>
<span class="sd">        &gt;&gt;&gt; phi.values</span>
<span class="sd">        array([ 14.,  22.,  30.])</span>
<span class="sd">        &gt;&gt;&gt; phi.variables</span>
<span class="sd">        [&#39;x2&#39;]</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">variables</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;variables: Expected type list or array-like, got type str&quot;</span><span class="p">)</span>

        <span class="n">phi</span> <span class="o">=</span> <span class="bp">self</span> <span class="k">if</span> <span class="n">inplace</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="n">variables</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">var</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{var}</span><span class="s2"> not in scope.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">var</span><span class="o">=</span><span class="n">var</span><span class="p">))</span>

        <span class="n">var_indexes</span> <span class="o">=</span> <span class="p">[</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">var</span><span class="p">)</span> <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="n">variables</span><span class="p">]</span>

        <span class="n">index_to_keep</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">)))</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">var_indexes</span><span class="p">))</span>
        <span class="n">phi</span><span class="o">.</span><span class="n">variables</span> <span class="o">=</span> <span class="p">[</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="n">index_to_keep</span><span class="p">]</span>
        <span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[</span><span class="n">index_to_keep</span><span class="p">]</span>

        <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="nb">tuple</span><span class="p">(</span><span class="n">var_indexes</span><span class="p">))</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">inplace</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">phi</span></div>

<div class="viewcode-block" id="DiscreteFactor.maximize"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.discrete.DiscreteFactor.DiscreteFactor.maximize">[docs]</a>    <span class="k">def</span> <span class="nf">maximize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">variables</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Maximizes the factor with respect to `variables`.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        variables: list, array-like</span>
<span class="sd">            List of variables with respect to which factor is to be maximized</span>

<span class="sd">        inplace: boolean</span>
<span class="sd">            If inplace=True it will modify the factor itself, else would return</span>
<span class="sd">            a new factor.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        DiscreteFactor or None: if inplace=True (default) returns None</span>
<span class="sd">                        if inplace=False returns a new `DiscreteFactor` instance.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; phi = DiscreteFactor([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;], [3, 2, 2], [0.25, 0.35, 0.08, 0.16, 0.05, 0.07,</span>
<span class="sd">        ...                                              0.00, 0.00, 0.15, 0.21, 0.09, 0.18])</span>
<span class="sd">        &gt;&gt;&gt; phi.variables</span>
<span class="sd">        [&#39;x1&#39;,&#39;x2&#39;,&#39;x3&#39;]</span>
<span class="sd">        &gt;&gt;&gt; phi.maximize([&#39;x2&#39;])</span>
<span class="sd">        &gt;&gt;&gt; phi.variables</span>
<span class="sd">        [&#39;x1&#39;, &#39;x3&#39;]</span>
<span class="sd">        &gt;&gt;&gt; phi.cardinality</span>
<span class="sd">        array([3, 2])</span>
<span class="sd">        &gt;&gt;&gt; phi.values</span>
<span class="sd">        array([[ 0.25,  0.35],</span>
<span class="sd">               [ 0.05,  0.07],</span>
<span class="sd">               [ 0.15,  0.21]])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">variables</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;variables: Expected type list or array-like, got type str&quot;</span><span class="p">)</span>

        <span class="n">phi</span> <span class="o">=</span> <span class="bp">self</span> <span class="k">if</span> <span class="n">inplace</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="n">variables</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">var</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{var}</span><span class="s2"> not in scope.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">var</span><span class="o">=</span><span class="n">var</span><span class="p">))</span>

        <span class="n">var_indexes</span> <span class="o">=</span> <span class="p">[</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">var</span><span class="p">)</span> <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="n">variables</span><span class="p">]</span>

        <span class="n">index_to_keep</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">)))</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">var_indexes</span><span class="p">))</span>
        <span class="n">phi</span><span class="o">.</span><span class="n">variables</span> <span class="o">=</span> <span class="p">[</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="n">index_to_keep</span><span class="p">]</span>
        <span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[</span><span class="n">index_to_keep</span><span class="p">]</span>

        <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="nb">tuple</span><span class="p">(</span><span class="n">var_indexes</span><span class="p">))</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">inplace</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">phi</span></div>

<div class="viewcode-block" id="DiscreteFactor.normalize"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.discrete.DiscreteFactor.DiscreteFactor.normalize">[docs]</a>    <span class="k">def</span> <span class="nf">normalize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Normalizes the values of factor so that they sum to 1.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        inplace: boolean</span>
<span class="sd">            If inplace=True it will modify the factor itself, else would return</span>
<span class="sd">            a new factor</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        DiscreteFactor or None: if inplace=True (default) returns None</span>
<span class="sd">                        if inplace=False returns a new `DiscreteFactor` instance.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; phi = DiscreteFactor([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;], [2, 3, 2], range(12))</span>
<span class="sd">        &gt;&gt;&gt; phi.values</span>
<span class="sd">        array([[[ 0,  1],</span>
<span class="sd">                [ 2,  3],</span>
<span class="sd">                [ 4,  5]],</span>

<span class="sd">               [[ 6,  7],</span>
<span class="sd">                [ 8,  9],</span>
<span class="sd">                [10, 11]]])</span>
<span class="sd">        &gt;&gt;&gt; phi.normalize()</span>
<span class="sd">        &gt;&gt;&gt; phi.variables</span>
<span class="sd">        [&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;]</span>
<span class="sd">        &gt;&gt;&gt; phi.cardinality</span>
<span class="sd">        array([2, 3, 2])</span>
<span class="sd">        &gt;&gt;&gt; phi.values</span>
<span class="sd">        array([[[ 0.        ,  0.01515152],</span>
<span class="sd">                [ 0.03030303,  0.04545455],</span>
<span class="sd">                [ 0.06060606,  0.07575758]],</span>

<span class="sd">               [[ 0.09090909,  0.10606061],</span>
<span class="sd">                [ 0.12121212,  0.13636364],</span>
<span class="sd">                [ 0.15151515,  0.16666667]]])</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">phi</span> <span class="o">=</span> <span class="bp">self</span> <span class="k">if</span> <span class="n">inplace</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

        <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">/</span> <span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">inplace</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">phi</span></div>

    <span class="nd">@StateNameDecorator</span><span class="p">(</span><span class="n">argument</span><span class="o">=</span><span class="s1">&#39;values&#39;</span><span class="p">,</span> <span class="n">return_val</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<div class="viewcode-block" id="DiscreteFactor.reduce"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.discrete.DiscreteFactor.DiscreteFactor.reduce">[docs]</a>    <span class="k">def</span> <span class="nf">reduce</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Reduces the factor to the context of given variable values.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        values: list, array-like</span>
<span class="sd">            A list of tuples of the form (variable_name, variable_state).</span>

<span class="sd">        inplace: boolean</span>
<span class="sd">            If inplace=True it will modify the factor itself, else would return</span>
<span class="sd">            a new factor.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        DiscreteFactor or None: if inplace=True (default) returns None</span>
<span class="sd">                        if inplace=False returns a new `DiscreteFactor` instance.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; phi = DiscreteFactor([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;], [2, 3, 2], range(12))</span>
<span class="sd">        &gt;&gt;&gt; phi.reduce([(&#39;x1&#39;, 0), (&#39;x2&#39;, 0)])</span>
<span class="sd">        &gt;&gt;&gt; phi.variables</span>
<span class="sd">        [&#39;x3&#39;]</span>
<span class="sd">        &gt;&gt;&gt; phi.cardinality</span>
<span class="sd">        array([2])</span>
<span class="sd">        &gt;&gt;&gt; phi.values</span>
<span class="sd">        array([0., 1.])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">values</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;values: Expected type list or array-like, got type str&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="p">(</span><span class="nb">any</span><span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">)</span> <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">values</span><span class="p">)</span> <span class="ow">or</span>
                <span class="ow">not</span> <span class="nb">all</span><span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">integer</span><span class="p">))</span> <span class="k">for</span> <span class="n">var</span><span class="p">,</span> <span class="n">state</span> <span class="ow">in</span> <span class="n">values</span><span class="p">)):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;values: must contain tuples or array-like elements of the form &quot;</span>
                            <span class="s2">&quot;(hashable object, type int)&quot;</span><span class="p">)</span>

        <span class="n">phi</span> <span class="o">=</span> <span class="bp">self</span> <span class="k">if</span> <span class="n">inplace</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

        <span class="n">var_index_to_del</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">slice_</span> <span class="o">=</span> <span class="p">[</span><span class="nb">slice</span><span class="p">(</span><span class="kc">None</span><span class="p">)]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">var</span><span class="p">,</span> <span class="n">state</span> <span class="ow">in</span> <span class="n">values</span><span class="p">:</span>
            <span class="n">var_index</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">var</span><span class="p">)</span>
            <span class="n">slice_</span><span class="p">[</span><span class="n">var_index</span><span class="p">]</span> <span class="o">=</span> <span class="n">state</span>
            <span class="n">var_index_to_del</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">var_index</span><span class="p">)</span>

        <span class="n">var_index_to_keep</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">)))</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">var_index_to_del</span><span class="p">))</span>
        <span class="c1"># set difference is not gaurenteed to maintain ordering</span>
        <span class="n">phi</span><span class="o">.</span><span class="n">variables</span> <span class="o">=</span> <span class="p">[</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="n">var_index_to_keep</span><span class="p">]</span>
        <span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[</span><span class="n">var_index_to_keep</span><span class="p">]</span>
        <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="nb">tuple</span><span class="p">(</span><span class="n">slice_</span><span class="p">)]</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">inplace</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">phi</span></div>

<div class="viewcode-block" id="DiscreteFactor.sum"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.discrete.DiscreteFactor.DiscreteFactor.sum">[docs]</a>    <span class="k">def</span> <span class="nf">sum</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">phi1</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        DiscreteFactor sum with `phi1`.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        phi1: `DiscreteFactor` instance.</span>
<span class="sd">            DiscreteFactor to be added.</span>

<span class="sd">        inplace: boolean</span>
<span class="sd">            If inplace=True it will modify the factor itself, else would return</span>
<span class="sd">            a new factor.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        DiscreteFactor or None: if inplace=True (default) returns None</span>
<span class="sd">                        if inplace=False returns a new `DiscreteFactor` instance.</span>

<span class="sd">        Example</span>
<span class="sd">        -------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; phi1 = DiscreteFactor([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;], [2, 3, 2], range(12))</span>
<span class="sd">        &gt;&gt;&gt; phi2 = DiscreteFactor([&#39;x3&#39;, &#39;x4&#39;, &#39;x1&#39;], [2, 2, 2], range(8))</span>
<span class="sd">        &gt;&gt;&gt; phi1.sum(phi2, inplace=True)</span>
<span class="sd">        &gt;&gt;&gt; phi1.variables</span>
<span class="sd">        [&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;, &#39;x4&#39;]</span>
<span class="sd">        &gt;&gt;&gt; phi1.cardinality</span>
<span class="sd">        array([2, 3, 2, 2])</span>
<span class="sd">        &gt;&gt;&gt; phi1.values</span>
<span class="sd">        array([[[[ 0,  0],</span>
<span class="sd">                 [ 4,  6]],</span>

<span class="sd">                [[ 0,  4],</span>
<span class="sd">                 [12, 18]],</span>

<span class="sd">                [[ 0,  8],</span>
<span class="sd">                 [20, 30]]],</span>


<span class="sd">               [[[ 6, 18],</span>
<span class="sd">                 [35, 49]],</span>

<span class="sd">                [[ 8, 24],</span>
<span class="sd">                 [45, 63]],</span>

<span class="sd">                [[10, 30],</span>
<span class="sd">                 [55, 77]]]])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">phi</span> <span class="o">=</span> <span class="bp">self</span> <span class="k">if</span> <span class="n">inplace</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">phi1</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">)):</span>
            <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">+=</span> <span class="n">phi1</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">phi1</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

            <span class="c1"># modifying phi to add new variables</span>
            <span class="n">extra_vars</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">extra_vars</span><span class="p">:</span>
                <span class="n">slice_</span> <span class="o">=</span> <span class="p">[</span><span class="nb">slice</span><span class="p">(</span><span class="kc">None</span><span class="p">)]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span>
                <span class="n">slice_</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">extra_vars</span><span class="p">))</span>
                <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="n">slice_</span><span class="p">]</span>

                <span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">extra_vars</span><span class="p">)</span>

                <span class="n">new_var_card</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">get_cardinality</span><span class="p">(</span><span class="n">extra_vars</span><span class="p">)</span>
                <span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span><span class="p">,</span> <span class="p">[</span><span class="n">new_var_card</span><span class="p">[</span><span class="n">var</span><span class="p">]</span> <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="n">extra_vars</span><span class="p">])</span>

            <span class="c1"># modifying phi1 to add new variables</span>
            <span class="n">extra_vars</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">extra_vars</span><span class="p">:</span>
                <span class="n">slice_</span> <span class="o">=</span> <span class="p">[</span><span class="nb">slice</span><span class="p">(</span><span class="kc">None</span><span class="p">)]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span>
                <span class="n">slice_</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">extra_vars</span><span class="p">))</span>
                <span class="n">phi1</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="n">slice_</span><span class="p">]</span>

                <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">extra_vars</span><span class="p">)</span>
                <span class="c1"># No need to modify cardinality as we don&#39;t need it.</span>

            <span class="c1"># rearranging the axes of phi1 to match phi</span>
            <span class="k">for</span> <span class="n">axis</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">ndim</span><span class="p">):</span>
                <span class="n">exchange_index</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">axis</span><span class="p">])</span>
                <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">axis</span><span class="p">],</span> <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">exchange_index</span><span class="p">]</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">exchange_index</span><span class="p">],</span> \
                    <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">axis</span><span class="p">]</span>
                <span class="n">phi1</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">swapaxes</span><span class="p">(</span><span class="n">axis</span><span class="p">,</span> <span class="n">exchange_index</span><span class="p">)</span>

            <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">+</span> <span class="n">phi1</span><span class="o">.</span><span class="n">values</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">inplace</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">phi</span></div>

<div class="viewcode-block" id="DiscreteFactor.product"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.discrete.DiscreteFactor.DiscreteFactor.product">[docs]</a>    <span class="k">def</span> <span class="nf">product</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">phi1</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        DiscreteFactor product with `phi1`.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        phi1: `DiscreteFactor` instance</span>
<span class="sd">            DiscreteFactor to be multiplied.</span>

<span class="sd">        inplace: boolean</span>
<span class="sd">            If inplace=True it will modify the factor itself, else would return</span>
<span class="sd">            a new factor.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        DiscreteFactor or None: if inplace=True (default) returns None</span>
<span class="sd">                        if inplace=False returns a new `DiscreteFactor` instance.</span>

<span class="sd">        Example</span>
<span class="sd">        -------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; phi1 = DiscreteFactor([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;], [2, 3, 2], range(12))</span>
<span class="sd">        &gt;&gt;&gt; phi2 = DiscreteFactor([&#39;x3&#39;, &#39;x4&#39;, &#39;x1&#39;], [2, 2, 2], range(8))</span>
<span class="sd">        &gt;&gt;&gt; phi1.product(phi2, inplace=True)</span>
<span class="sd">        &gt;&gt;&gt; phi1.variables</span>
<span class="sd">        [&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;, &#39;x4&#39;]</span>
<span class="sd">        &gt;&gt;&gt; phi1.cardinality</span>
<span class="sd">        array([2, 3, 2, 2])</span>
<span class="sd">        &gt;&gt;&gt; phi1.values</span>
<span class="sd">        array([[[[ 0,  0],</span>
<span class="sd">                 [ 4,  6]],</span>

<span class="sd">                [[ 0,  4],</span>
<span class="sd">                 [12, 18]],</span>

<span class="sd">                [[ 0,  8],</span>
<span class="sd">                 [20, 30]]],</span>


<span class="sd">               [[[ 6, 18],</span>
<span class="sd">                 [35, 49]],</span>

<span class="sd">                [[ 8, 24],</span>
<span class="sd">                 [45, 63]],</span>

<span class="sd">                [[10, 30],</span>
<span class="sd">                 [55, 77]]]]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">phi</span> <span class="o">=</span> <span class="bp">self</span> <span class="k">if</span> <span class="n">inplace</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">phi1</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">)):</span>
            <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">*=</span> <span class="n">phi1</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">phi1</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

            <span class="c1"># modifying phi to add new variables</span>
            <span class="n">extra_vars</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">extra_vars</span><span class="p">:</span>
                <span class="n">slice_</span> <span class="o">=</span> <span class="p">[</span><span class="nb">slice</span><span class="p">(</span><span class="kc">None</span><span class="p">)]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span>
                <span class="n">slice_</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">extra_vars</span><span class="p">))</span>
                <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="n">slice_</span><span class="p">]</span>

                <span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">extra_vars</span><span class="p">)</span>

                <span class="n">new_var_card</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">get_cardinality</span><span class="p">(</span><span class="n">extra_vars</span><span class="p">)</span>
                <span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span><span class="p">,</span> <span class="p">[</span><span class="n">new_var_card</span><span class="p">[</span><span class="n">var</span><span class="p">]</span> <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="n">extra_vars</span><span class="p">])</span>

            <span class="c1"># modifying phi1 to add new variables</span>
            <span class="n">extra_vars</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">extra_vars</span><span class="p">:</span>
                <span class="n">slice_</span> <span class="o">=</span> <span class="p">[</span><span class="nb">slice</span><span class="p">(</span><span class="kc">None</span><span class="p">)]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span>
                <span class="n">slice_</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">extra_vars</span><span class="p">))</span>
                <span class="n">phi1</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="n">slice_</span><span class="p">]</span>

                <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">extra_vars</span><span class="p">)</span>
                <span class="c1"># No need to modify cardinality as we don&#39;t need it.</span>

            <span class="c1"># rearranging the axes of phi1 to match phi</span>
            <span class="k">for</span> <span class="n">axis</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">ndim</span><span class="p">):</span>
                <span class="n">exchange_index</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">axis</span><span class="p">])</span>
                <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">axis</span><span class="p">],</span> <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">exchange_index</span><span class="p">]</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">exchange_index</span><span class="p">],</span> \
                    <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">axis</span><span class="p">]</span>
                <span class="n">phi1</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">swapaxes</span><span class="p">(</span><span class="n">axis</span><span class="p">,</span> <span class="n">exchange_index</span><span class="p">)</span>

            <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">*</span> <span class="n">phi1</span><span class="o">.</span><span class="n">values</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">inplace</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">phi</span></div>

<div class="viewcode-block" id="DiscreteFactor.divide"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.discrete.DiscreteFactor.DiscreteFactor.divide">[docs]</a>    <span class="k">def</span> <span class="nf">divide</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">phi1</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        DiscreteFactor division by `phi1`.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        phi1 : `DiscreteFactor` instance</span>
<span class="sd">            The denominator for division.</span>

<span class="sd">        inplace: boolean</span>
<span class="sd">            If inplace=True it will modify the factor itself, else would return</span>
<span class="sd">            a new factor.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        DiscreteFactor or None: if inplace=True (default) returns None</span>
<span class="sd">                        if inplace=False returns a new `DiscreteFactor` instance.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; phi1 = DiscreteFactor([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;], [2, 3, 2], range(12))</span>
<span class="sd">        &gt;&gt;&gt; phi2 = DiscreteFactor([&#39;x3&#39;, &#39;x1&#39;], [2, 2], range(1, 5)])</span>
<span class="sd">        &gt;&gt;&gt; phi1.divide(phi2)</span>
<span class="sd">        &gt;&gt;&gt; phi1.variables</span>
<span class="sd">        [&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;]</span>
<span class="sd">        &gt;&gt;&gt; phi1.cardinality</span>
<span class="sd">        array([2, 3, 2])</span>
<span class="sd">        &gt;&gt;&gt; phi1.values</span>
<span class="sd">        array([[[ 0.        ,  0.33333333],</span>
<span class="sd">                [ 2.        ,  1.        ],</span>
<span class="sd">                [ 4.        ,  1.66666667]],</span>

<span class="sd">               [[ 3.        ,  1.75      ],</span>
<span class="sd">                [ 4.        ,  2.25      ],</span>
<span class="sd">                [ 5.        ,  2.75      ]]])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">phi</span> <span class="o">=</span> <span class="bp">self</span> <span class="k">if</span> <span class="n">inplace</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">phi1</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

        <span class="k">if</span> <span class="nb">set</span><span class="p">(</span><span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Scope of divisor should be a subset of dividend&quot;</span><span class="p">)</span>

        <span class="c1"># Adding extra variables in phi1.</span>
        <span class="n">extra_vars</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">extra_vars</span><span class="p">:</span>
            <span class="n">slice_</span> <span class="o">=</span> <span class="p">[</span><span class="nb">slice</span><span class="p">(</span><span class="kc">None</span><span class="p">)]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span>
            <span class="n">slice_</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">extra_vars</span><span class="p">))</span>
            <span class="n">phi1</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="n">slice_</span><span class="p">]</span>

            <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">extra_vars</span><span class="p">)</span>

        <span class="c1"># Rearranging the axes of phi1 to match phi</span>
        <span class="k">for</span> <span class="n">axis</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">ndim</span><span class="p">):</span>
            <span class="n">exchange_index</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">axis</span><span class="p">])</span>
            <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">axis</span><span class="p">],</span> <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">exchange_index</span><span class="p">]</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">exchange_index</span><span class="p">],</span> <span class="n">phi1</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">axis</span><span class="p">]</span>
            <span class="n">phi1</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">phi1</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">swapaxes</span><span class="p">(</span><span class="n">axis</span><span class="p">,</span> <span class="n">exchange_index</span><span class="p">)</span>

        <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">/</span> <span class="n">phi1</span><span class="o">.</span><span class="n">values</span>

        <span class="c1"># If factor division 0/0 = 0 but is undefined for x/0. In pgmpy we are using</span>
        <span class="c1"># np.inf to represent x/0 cases.</span>
        <span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="p">)]</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">inplace</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">phi</span></div>

<div class="viewcode-block" id="DiscreteFactor.copy"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.discrete.DiscreteFactor.DiscreteFactor.copy">[docs]</a>    <span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a copy of the factor.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        DiscreteFactor: copy of the factor</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; phi = DiscreteFactor([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;], [2, 3, 3], np.arange(18))</span>
<span class="sd">        &gt;&gt;&gt; phi_copy = phi.copy()</span>
<span class="sd">        &gt;&gt;&gt; phi_copy.variables</span>
<span class="sd">        [&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;]</span>
<span class="sd">        &gt;&gt;&gt; phi_copy.cardinality</span>
<span class="sd">        array([2, 3, 3])</span>
<span class="sd">        &gt;&gt;&gt; phi_copy.values</span>
<span class="sd">        array([[[ 0,  1,  2],</span>
<span class="sd">                [ 3,  4,  5],</span>
<span class="sd">                [ 6,  7,  8]],</span>

<span class="sd">               [[ 9, 10, 11],</span>
<span class="sd">                [12, 13, 14],</span>
<span class="sd">                [15, 16, 17]]])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># not creating a new copy of self.values and self.cardinality</span>
        <span class="c1"># because __init__ methods does that.</span>
        <span class="k">return</span> <span class="n">DiscreteFactor</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">scope</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">is_valid_cpd</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">allclose</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">to_factor</span><span class="p">()</span><span class="o">.</span><span class="n">marginalize</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">scope</span><span class="p">()[:</span><span class="mi">1</span><span class="p">],</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">flatten</span><span class="p">(</span><span class="s1">&#39;C&#39;</span><span class="p">),</span>
                            <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[:</span><span class="mi">0</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">])),</span>
                            <span class="n">atol</span><span class="o">=</span><span class="mf">0.01</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY2</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_str</span><span class="p">(</span><span class="n">phi_or_p</span><span class="o">=</span><span class="s1">&#39;phi&#39;</span><span class="p">,</span> <span class="n">tablefmt</span><span class="o">=</span><span class="s2">&quot;psql&quot;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_str</span><span class="p">(</span><span class="n">phi_or_p</span><span class="o">=</span><span class="s1">&#39;phi&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_str</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">phi_or_p</span><span class="o">=</span><span class="s2">&quot;phi&quot;</span><span class="p">,</span> <span class="n">tablefmt</span><span class="o">=</span><span class="s2">&quot;fancy_grid&quot;</span><span class="p">,</span> <span class="n">print_state_names</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generate the string from `__str__` method.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        phi_or_p: &#39;phi&#39; | &#39;p&#39;</span>
<span class="sd">                &#39;phi&#39;: When used for Factors.</span>
<span class="sd">                  &#39;p&#39;: When used for CPDs.</span>
<span class="sd">        print_state_names: boolean</span>
<span class="sd">                If True, the user defined state names are displayed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">string_header</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">scope</span><span class="p">()))</span>
        <span class="n">string_header</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{phi_or_p}</span><span class="s1">(</span><span class="si">{variables}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">phi_or_p</span><span class="o">=</span><span class="n">phi_or_p</span><span class="p">,</span>
                                                              <span class="n">variables</span><span class="o">=</span><span class="s1">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">string_header</span><span class="p">)))</span>

        <span class="n">value_index</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">factor_table</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">prob</span> <span class="ow">in</span> <span class="n">product</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="nb">range</span><span class="p">(</span><span class="n">card</span><span class="p">)</span> <span class="k">for</span> <span class="n">card</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span><span class="p">]):</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_names</span> <span class="ow">and</span> <span class="n">print_state_names</span><span class="p">:</span>
                <span class="n">prob_list</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;</span><span class="si">{var}</span><span class="s2">(</span><span class="si">{state}</span><span class="s2">)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">var</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">)[</span><span class="n">i</span><span class="p">],</span> <span class="n">state</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">state_names</span><span class="p">[</span><span class="nb">list</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">)[</span><span class="n">i</span><span class="p">]][</span><span class="n">prob</span><span class="p">[</span><span class="n">i</span><span class="p">]])</span>
                             <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">))]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">prob_list</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;</span><span class="si">{s}</span><span class="s2">_</span><span class="si">{d}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">s</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">)[</span><span class="n">i</span><span class="p">],</span> <span class="n">d</span><span class="o">=</span><span class="n">prob</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
                             <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">))]</span>

            <span class="n">prob_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">ravel</span><span class="p">()[</span><span class="n">value_index</span><span class="p">])</span>
            <span class="n">factor_table</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">prob_list</span><span class="p">)</span>
            <span class="n">value_index</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="k">return</span> <span class="n">tabulate</span><span class="p">(</span><span class="n">factor_table</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="n">string_header</span><span class="p">,</span> <span class="n">tablefmt</span><span class="o">=</span><span class="n">tablefmt</span><span class="p">,</span> <span class="n">floatfmt</span><span class="o">=</span><span class="s2">&quot;.4f&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">var_card</span> <span class="o">=</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s1">&#39;</span><span class="si">{var}</span><span class="s1">:</span><span class="si">{card}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">var</span><span class="o">=</span><span class="n">var</span><span class="p">,</span> <span class="n">card</span><span class="o">=</span><span class="n">card</span><span class="p">)</span>
                              <span class="k">for</span> <span class="n">var</span><span class="p">,</span> <span class="n">card</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span><span class="p">)])</span>
        <span class="k">return</span> <span class="s2">&quot;&lt;DiscreteFactor representing phi(</span><span class="si">{var_card}</span><span class="s2">) at </span><span class="si">{address}</span><span class="s2">&gt;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="n">address</span><span class="o">=</span><span class="nb">hex</span><span class="p">(</span><span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">)),</span> <span class="n">var_card</span><span class="o">=</span><span class="n">var_card</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__mul__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__rmul__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__mul__</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__add__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__radd__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__add__</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__truediv__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">divide</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

    <span class="n">__div__</span> <span class="o">=</span> <span class="n">__truediv__</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">DiscreteFactor</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">DiscreteFactor</span><span class="p">)):</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="k">elif</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">scope</span><span class="p">())</span> <span class="o">!=</span> <span class="nb">set</span><span class="p">(</span><span class="n">other</span><span class="o">.</span><span class="n">scope</span><span class="p">()):</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="n">phi</span> <span class="o">=</span> <span class="n">other</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">axis</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">ndim</span><span class="p">):</span>
                <span class="n">exchange_index</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">axis</span><span class="p">])</span>
                <span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">axis</span><span class="p">],</span> <span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">exchange_index</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">exchange_index</span><span class="p">],</span>
                                                                      <span class="n">phi</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="n">axis</span><span class="p">])</span>
                <span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[</span><span class="n">axis</span><span class="p">],</span> <span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[</span><span class="n">exchange_index</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[</span><span class="n">exchange_index</span><span class="p">],</span>
                                                                          <span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[</span><span class="n">axis</span><span class="p">])</span>
                <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">swapaxes</span><span class="p">(</span><span class="n">axis</span><span class="p">,</span> <span class="n">exchange_index</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">shape</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">shape</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">False</span>
            <span class="k">elif</span> <span class="ow">not</span> <span class="n">np</span><span class="o">.</span><span class="n">allclose</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="p">):</span>
                <span class="k">return</span> <span class="kc">False</span>
            <span class="k">elif</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span> <span class="o">==</span> <span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span><span class="p">):</span>
                <span class="k">return</span> <span class="kc">False</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">__ne__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">__eq__</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">variable_hashes</span> <span class="o">=</span> <span class="p">[</span><span class="nb">hash</span><span class="p">(</span><span class="n">variable</span><span class="p">)</span> <span class="k">for</span> <span class="n">variable</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">]</span>
        <span class="n">sorted_var_hashes</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">variable_hashes</span><span class="p">)</span>
        <span class="n">phi</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">axis</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">ndim</span><span class="p">):</span>
            <span class="n">exchange_index</span> <span class="o">=</span> <span class="n">variable_hashes</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">sorted_var_hashes</span><span class="p">[</span><span class="n">axis</span><span class="p">])</span>
            <span class="n">variable_hashes</span><span class="p">[</span><span class="n">axis</span><span class="p">],</span> <span class="n">variable_hashes</span><span class="p">[</span><span class="n">exchange_index</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">variable_hashes</span><span class="p">[</span><span class="n">exchange_index</span><span class="p">],</span>
                                                                      <span class="n">variable_hashes</span><span class="p">[</span><span class="n">axis</span><span class="p">])</span>
            <span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[</span><span class="n">axis</span><span class="p">],</span> <span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[</span><span class="n">exchange_index</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[</span><span class="n">exchange_index</span><span class="p">],</span>
                                                                      <span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[</span><span class="n">axis</span><span class="p">])</span>
            <span class="n">phi</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">swapaxes</span><span class="p">(</span><span class="n">axis</span><span class="p">,</span> <span class="n">exchange_index</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">hash</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">sorted_var_hashes</span><span class="p">)</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">values</span><span class="p">)</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">phi</span><span class="o">.</span><span class="n">cardinality</span><span class="p">))</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2016, Ankur Ankan.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.1.
    </div>
  </body>
</html>